package com.ellricky.baselib.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Space;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatRatingBar;
import androidx.fragment.app.Fragment;

import com.andr.common.ui.adapter.BaseRecyclerAdapter;
import com.andr.common.ui.adapter.SmartViewHolder;
import com.andr.common.util.EmptyUtil;
import com.andr.common.util.StrUtil;
import com.ellricky.baselib.R;
import com.ellricky.baselib.entity.PersonItemEntity;
import com.ellricky.baselib.listener.PersonListListener;
import com.ellricky.baselib.util.EAppUtil;
import com.ellricky.baselib.util.GlideUtil;

import de.hdodenhof.circleimageview.CircleImageView;

public class PersonItemAdapter extends BaseRecyclerAdapter<PersonItemEntity> {

    private Activity activity;
    private Fragment fragment;
    private PersonListListener personListListener;

    // 选择人员
    private boolean choosePage = false;
    private boolean showRateBar = false;
    private boolean showClassNum = false;
    private boolean showSex = false;

    public void setShowSex(boolean showSex) {
        this.showSex = showSex;
    }

    public void setShowRateBar(boolean showRateBar) {
        this.showRateBar = showRateBar;
    }

    public void setChoosePage(boolean choosePage) {
        this.choosePage = choosePage;
    }

    public void setShowClassNum(boolean showClassNum) {
        this.showClassNum = showClassNum;
    }

    public void setPersonListListener(PersonListListener personListListener) {
        this.personListListener = personListListener;
    }

    public PersonItemAdapter(Activity activity, Fragment fragment) {
        super(R.layout.base_layout_adapter_person_item);

        if (fragment != null) {
            this.fragment = fragment;
            this.activity = fragment.getActivity();
        }

        if (activity != null) {
            this.activity = activity;
        }
    }


    @Override
    public void onBindViewHolder(SmartViewHolder holder, PersonItemEntity model, final int position) {
        View itemView = holder.itemView;

        CircleImageView iv = itemView.findViewById(R.id.ivPic);
        TextView tvName = itemView.findViewById(R.id.tvName);

        ImageView ivState = itemView.findViewById(R.id.ivStateLabel);
        final TextView cb = itemView.findViewById(R.id.cbSel);

        Space space = itemView.findViewById(R.id.space);
        AppCompatRatingBar rBar = itemView.findViewById(R.id.rateBar);
        TextView tvClassNum = itemView.findViewById(R.id.tvClassNum);

        // 头像
        String picUrl = model.getHeadPic();
        if (!TextUtils.isEmpty(picUrl)) {
            GlideUtil.into(activity, picUrl, iv);
        }

        // 姓名+年龄
        String name = StrUtil.filterEmpty(model.getName());
        String sexName = StrUtil.filterEmpty(model.getSexName());
        if (showSex && !EmptyUtil.isEmpty(sexName)) {
            name += "(" + sexName + ")";
        }
        tvName.setText(name);

        // 请假状态图标
        String workStatus = model.getWorkStatus();
        if (!EmptyUtil.isEmpty(workStatus)) {
            if ("2".equals(workStatus)) {
                ivState.setVisibility(View.VISIBLE);
            } else {
                ivState.setVisibility(View.GONE);
            }
        }

        // 评估状态
        String attendType = model.getAttendType();
        if (!EmptyUtil.isEmpty(attendType)) {
            if ("1".equals(attendType)) {
                ivState.setVisibility(View.VISIBLE);
                ivState.setImageResource(R.drawable.base_icon_evaluate);
            } else {
                ivState.setVisibility(View.GONE);
            }
        }

        // 考勤状态
        String isAttend = model.getIsAttend();
        if (!EmptyUtil.isEmpty(isAttend)) {
            if ("0".equals(model.getIsAttend())) {
                ivState.setVisibility(View.GONE);
            } else {
                ivState.setVisibility(View.VISIBLE);
                ivState.setImageResource(R.drawable.base_icon_attended);
            }
        }


        // 选择人员
        if (choosePage) {
            cb.setVisibility(View.VISIBLE);
            cb.setSelected(model.isChecked());
            final PersonItemEntity itemEntity = model;
            cb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    cb.setSelected(!cb.isSelected());
                    itemEntity.setChecked(cb.isSelected());
                    if (personListListener != null)
                        personListListener.onClickPersonItem(itemEntity);
                }
            });
        } else {
            cb.setVisibility(View.GONE);
        }

        if (showRateBar) {
            String userLevel = model.getUserLevel();
            space.setVisibility(View.GONE);
            rBar.setVisibility(View.VISIBLE);
            EAppUtil.setUserLevel(rBar, userLevel);
        } else {
            space.setVisibility(View.VISIBLE);
            rBar.setVisibility(View.GONE);
        }

        // 课时数
        if (showClassNum) {
            if ("0".equals(model.getIsLeave())) {
                ivState.setVisibility(View.GONE);
            } else {
                ivState.setVisibility(View.VISIBLE);
            }

            String classNum = model.getClassNum();
            if (EmptyUtil.isEmpty(classNum)) {
                space.setVisibility(View.VISIBLE);
                tvClassNum.setVisibility(View.GONE);
            } else {
                space.setVisibility(View.GONE);
                tvClassNum.setVisibility(View.VISIBLE);
                tvClassNum.setText(
                        String.format("%s课时", StrUtil.filterEmpty(classNum))
                );
            }
        }


    }
}
